package com.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

//@SpringBootApplication
public class ZookeeperApplication {

	public static void main(String[] args) throws Exception {
//		SpringApplication.run(ZookeeperApplication.class, args);
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);

		client.start();
		System.out.println("已经成功的连接ZK服务了....");


		client.create().creatingParentsIfNeeded()
				.withMode(CreateMode.PERSISTENT)
				.forPath("/my/path","100".getBytes());


		byte[] firstData = client.getData().forPath("/my/path");
		System.out.println("firstData:" + new String(firstData));


		client.setData().forPath("/my/path", "100".getBytes());
		byte[] dataBytes = client.getData().forPath("/my/path");
		System.out.println(new String(dataBytes));


		client.setData().forPath("/my/path", "110".getBytes());
		byte[] dataBytes1 = client.getData().forPath("/my/path");
		System.out.println(new String(dataBytes1));


		List<String> children = client.getChildren().forPath("/my");
		System.out.println(children);


		client.delete().forPath("/my/path");


		client.close();
	}

}
